Skip to main content

MIME Types

MIME (Multipurpose Internet Mail Extensions) types tell the client (browser) what kind of content is being sent, so the client knows how to handle or render it.

Format: type/subtype

Examples

MIME TypeMeaning
text/htmlHTML page
text/cssCSS
application/javascriptJavaScript
image/pngPNG image
application/pdfPDF file

Why MIME Types Matter in NGINX​

Correct MIME types ensure:

  • Proper rendering in browsers
  • Correct file downloads
  • Security (avoid content sniffing)
  • Compatibility with clients and APIs

Wrong MIME types can cause:

  • ❌ Files downloading instead of rendering
  • ❌ Broken web pages
  • ❌ Security warnings

How NGINX Determines MIME Types​

NGINX determines the Content-Type header using this order:

  1. Explicit types mapping
  2. Included mime.types file
  3. default_type fallback

Core Directives for MIME Types​

DirectiveContextPurpose
typeshttp, server, locationMap extensions → MIME types
include mime.typeshttpLoad standard mappings
default_typehttp, server, locationFallback MIME type
http {
include mime.types;
default_type application/octet-stream;
}
  • mime.types contains hundreds of standard mappings
  • application/octet-stream forces download for unknown files

The mime.types File Explained​

Typical Location

/etc/nginx/mime.types

Example Content

types {
text/html html;
text/css css;
application/javascript js;
image/jpeg jpg jpeg;
image/png png;
application/json json;
}
  • Multiple extensions can map to one MIME type
  • NGINX matches by file extension

MIME Type Resolution Example​

Config

root /var/www/html;

Files

/var/www/html/
├── index.html
├── style.css
├── app.js
├── logo.png
├── data.json

Client Receives

FileContent-Type
index.htmltext/html
style.csstext/css
app.jsapplication/javascript
logo.pngimage/png
data.jsonapplication/json

default_type Directive​

Syntax

default_type mime-type;

Example

default_type text/plain;
  • Used when no MIME match exists
  • Overrides default behavior if defined in lower context

Overriding MIME Types (Custom Mapping)​

Example: Force .log Files to Download

types {
text/plain log;
}

Example: Force File Download

location /downloads/ {
default_type application/octet-stream;
}

MIME Types Per Location​

location /media/ {
types {
video/mp4 mp4;
}
}
  • Overrides MIME handling only for /media/

Disabling MIME Type Detection​

types { }
default_type application/octet-stream;
  • All files downloaded
  • Useful for secure file servers

Security Best Practices​

Prevent MIME Sniffing​

add_header X-Content-Type-Options nosniff;

Prevents browsers from guessing content type.

Correct JavaScript MIME Type​

application/javascript js;

Avoids browser console warnings.

ProblemCause
CSS not loadingWrong MIME type
JS blockedtext/plain instead of JS
Images downloadIncorrect mapping
Browser warningsMissing nosniff

Debugging MIME Types​

Check Headers

curl -I http://example.com/style.css

Expected:

Content-Type: text/css

Real-World Production Example​

http {
include mime.types;
default_type application/octet-stream;

server {
listen 80;
root /var/www/app;

location / {
try_files $uri $uri/ =404;
}

add_header X-Content-Type-Options nosniff;
}
}

Common Mistakes​

  • Not including mime.types
  • Wrong JS MIME (text/javascript in strict setups)
  • Overriding default_type globally
  • Relying on browser sniffing